Databases



Learning Objectives : Student should be able to -


Q1.  What is  Database ?

⇒  A database is a computerized record-keeping system.

⇒  It is an organized collection of data, that allows people to quickly search and extract information in a way that meets their need; edit and delete records using computer software.

⇒  The data can include text, numbers, pictures or anything that can be stored in a computer.

Q2.  Describe the following three types of database.

1)  Single-table database (or) Flat database.

⇒  A Single-table database is a simple database that stores all data in a single table in rows and columns, also called as Flat database.

⇒  It contains unrelated tables like spreadsheet. It can be converted into text file separating each field using delimiters like comma, hyphen etc.

2)  Relational database.

⇒  A computerized record-keeping system that contains two or more tables of data, interrelated through key fields called relationships.

⇒  Relational database are very flexible for accessing, managing, and updating data.

3)  Distributed database.

⇒  Distributed database is relational database distributed over a computer network allowing multiple users to access data simultaneously.

Q3.  State the advantages and disadvantages of using database.

✬   Advantages 

⇒  Avoid data redundancy (duplication of data).

⇒  Increased data consistency (or correctness), allows only valid data defined by some rules.

⇒  Improve data access to users through use of host and query languages.

⇒  Improved data security to access the data through user credentials.

⇒  A database can be shared with number of users over the network.

✬   Disdvantages 

⇒  Database systems are complex, difficult, and time-consuming to design.

⇒  Extensive conversion costs in moving from a file-based system to a database system.

⇒  Initial training required for all programmers and users.

Q4.  Give three uses of database.

  1. To store information about people, like pupils at school, patients in a hospital, etc.
  2. To store information about things, like cars to be sold, books in a library, etc.
  3. To store information about events, like hotel bookings, result of races, etc.

Q5. a)  What is (DBMS) Database Management System.

⇒  A database management system is a set of programs that allows the creation of data files called tables and permits the ways to process, alter and extract records.

It provides the following features :

b)  Describe the following four main components of database management system.

✬    Table  :  A Table is a data structure, a basic unit of storing data in a database. It stores related data in rows called records with different piece of information in columns called fields of a record.

✬    Form  :  A Form is a database object, created for the user to enter, edit or display data from the data source table. It provides a user friendly interface for a database application.

✬    Query  :  A Query is a request used to extract data from a database according to the user’s request. It uses structured query language (SQL) that can be written or designed through query design grids.

✬    Report  :  A Report is an database object used to display and print formatted data in an organized manner form the database table.

Q6. a)  What is meant by  Record  and  Field  ?

⇒  Records is a basic data structure composed of different fields in a rows. Each record is a single row in the table that contains different information of an item. A set of records makes a database table.

⇒  Field refers to the columns, or data categories of same data type used by each record. It represents an attribute (a characteristic or quality) of some entity (like object, person, place, or event).

Records and Fields>

b)  Describe  Primary Key-field .

⇒  A primary key is a field in a table which uniquely identifies each record in a database table.

⇒  Primary keys must contain unique values.

⇒  A primary key column cannot have NULL values or left empty.

c)  Describe two purpose of primary key-field.

Q7.  Describe how to create or design a database Table.

  1. Organize data of each individual or thing in rows, with each column containing data of same data type.
  2. In table design view, name each field with its appropriate data type.
  3. Add validation check by specifying proper attributes like size (length of field), format, validation rule etc.
  4. Identify the field which uniquely identifies each record and specify it as primary key field.
  5. Save the table structure to store data later using datasheet view or form.

Q8.  Describe the following  Fields attributes (or properties)  that need to be decided before setting up the database to control how they receive data.

 Field name :  The names you give to the columns in a table to identify the field, so it is mandatory.
 Field / Data type :  Determines the type of data that a user can enter into the field. It validates and restricts invalid data types. Based on data-type other properties of the fileds are provided.
 Field size :  Specifies the required length or size of data. It helps to minimize the wastage of storage space.
 Format :  Specifies a unique format of data to be allowed and stored in a uniform way.
 Input Mask :  It is a string of characters that indicates the format of valid input values.
 Validation Rule :  Specifies the criteria or rule that should be met before storing data in the field.

Q9. a)  Describe different  data types  used in designing a table.

Data type Description Example
Text It allows alphanumeric data, like text, numbers and symbols. Name : James
Address : 73, High Street.
Postal code : CV34 5TR
Character It allows only a single character of any alphanumeric data. Code : N / C / P . . .
House : R / B / G / Y . . .
Category : D / W / V . . .
Integer It allows only a positive or negative whole numbers only. Age : 16
Quantity : 36
Temperature : - 10
Real It allows only a positive or negative decimal numbers. Lenth (cm) : 12.5
Weight (kg) : 52.36
Temperature : - 2.5
Currency It allows price with currency symbols and ensures there are two decimal places. Cost : $5.75
Amount : £468.25
Price : ₹18.75
Date/Time It checks and allows only date and time in an appropriate formate. Long Date : 20 February 2006  18:21:35
Medium Date : 20-Feb-06  06:21 PM
Short Date : 20/02/06  18:21
Boolean It allows only any one of the two choices. Paid : Yes / No
Option : True / False
Gender : Male / Female

b)  Give two reasons why choosing the correct data-type is important.

  1. Do validation check over data while entering to prevent invalid characters being stored.
  2. To check the correct format of data, like date and time to be stored in uniform way.

c)  Give two reasons why setting the field-size or maximum length of a field is important.

  1. File size can be kept as small as possible.
  2. Time taken to process data can be kept minimum.

Q10.  Codes are used to represent information and are useful, provided we know how to interpret them.

a)  Why Coding data is useful?

⇒  Codes are quicker to type in.

⇒  Using codes reduces the size of the files.

⇒  Increases the speed of search and process performed on the data.

⇒  Codes are often unique and are easy to search.

b)  Give three important factors that should be taken into account when designing codes.

  1. Code should always be of same length.
  2. Codes should contain enough details and should not be too short.
  3. Codes should be easy to use.

Q11. a)  Describe what is meant by  Data Validation .

⇒  Validation is an automatic computer check to ensure that the data entered is sensible, reasonable and consistent. Data is accepted only if it falls withing the set rules. It does not check the accuracy of data.

b)  Describe how the following Validation-check works with example of its usage.

Validation type How it works Example usage
Presence check Checks that data has been entered into a field. Field which cannot be left blank like primary key field of a database.
Data-type check Ensure that the correct type of data (like text, number, date) is entered into that field. Name (Text), Date of birth (Date), Age (Number), Price (Currency).
Length check Checks the data isn't too short or too long. A password which needs to be six letters long.
Format check Checks the data is in the right format. Passport number is in the form M5228907 where M is a letter followed by 7 digits.
Date format check Checks the date is in the right format. Date of birth is in the form 20 Jan. 1998 or 20/01/1998
Check digit The last one digit in a code (number) is used to check the other digits are correct. Bar code use check digits at right most of the number to check its integrity.
List check (or) Lookup table Looks up acceptable values in a list or table. There are only seven possible days of the week.
Range check Checks that a value falls within the specified range of values. Number of hours worked must be less than 50 and more than 0.
Consistency (or) Cross-field check Checks that different fields in the same record correspond correctly. If ‘Mr’ has been entered into the title field, ‘Male’ must be entered into the gender field.
Spell check Looks up words in a dictionary. When word processing.

Q12. a)  What is meant by  SQL  query in database ?

⇒  SQL stands for Structured Query Language. It is a standard query language for writing scripts, to make request to the database for the records which meets the criteria we specify.

⇒  An SQL script is a list of SQL commands that performs a given task, often stored in a file so the script can be reused.

b)  State the benefits of using a query.

  1. View data only from the fields you are interested in viewing.
  2. Combine data from several data sources.
  3. Use expressions as fields.
  4. View records that meet criteria that you specify.

c)  Explain the function of each of the following SQL statements.

(i) SELECT : Fetches specified fields from the table; queries always begins with SELECT.

A SELECT statement takes the form :

SELECT Field1, Field2, Field3, etc. – this specifies the individual fields to be shown.

SELECT * - this specifies that all the fields are to be shown.

(ii) FROM : Identifies the table to use.

A FROM statement takes the form :

FROM TableName – this specifies the table to use.

(iii) WHERE : Includes only records in a query that match a given condition.

A WHERE statement takes the form :

WHERE Condition – this specifies the condition to apply. Conditions often include values from fields, these values need to match the data type for the field.

(iv) ORDER BY : Sorts the results from a query by a given column either alphabetically or numerically.

A ORDER BY statement takes the form :

ORDER BY Field1, Field2 – this specifies a sort in ascending or alphabetical order starting with the first field and then the second field.

ORDER BY Field1 DESC, Field2 – this specifies a sort in descending or reverse order starting with the first field and ascending order of second field.

Note : The key-word DESC is used to specify descending order only, to sort in ascending order, we should not use this keyword.

(v) SUM : Returns the sum of all the values in a field. Used with SELECT.

A SUM statement takes the form :

SELECT SUM(Field) – this returns the sum of values in the specified field as argument. The field should be of data type, integer or real.

SELECT SUM(Field) AS ResultField - The name of the new result field is optional (i.e. the alias).

(vi) COUNT : Counts the number of records where the field matches a specified condition. Used with SELECT.

A COUNT statement takes the form :

SELECT COUNT(Field) – this returns the number of items in the specified field as argument, that meets the given criteria.

SELECT COUNT (Field) AS ResultField - The name of the new result field is optional (i.e. the alias).

Q13.  Describe how to create SQL query to retrieve data from database table.

  1. SQL query always begins with SELECT followed by FROM commands, which are mandatory in an SQL script.
  2. All other commands are optional.
  3. Start SQL script with SELECT command line, identify and write the name of fields to display separated by Comma (,).
  4. Next specify the table-name in FROM command line.
  5. Write the criteria or condition in WHERE command line.
  6. Specify the name of the fields to sort in ORDER BY command line.
  7. Put Semicolon ( ; ), to mark the end the SQL statement.

Examples of criteria or condition to specify in WHERE command line :

Criteria Query result
= "Male"
// or //
Like "Male"
Returns records where the field exactly match the value Male.
Not "Apple"   // or //
Not Like "Apple"
Returns records where the field do not match the value Apple.
Like "U * " Returns records where the field value (text) begins with the string "U", such as UK, USA and so on.
Asterisk sign (*) is a "wildcard" that denotes any number of characters including blank-space.
Not Like "U * " Returns records where the field value (text) do not begin with the string "U".
"Apple" OR "Orange" Returns records where the field value (text) match one of two values, like records with both "Apple" and "Orange".
100    // or //
= 100
Returns records where the field value is equal to 100.
Not 100   // or //
< > 100
Returns records where the field value is not equal to 100.
> 50 Returns records where the field value is greater than 50.
>= 50 Returns records where the field value is greater than or equal to 50.
< 75 Returns records where the field value is less than 75.
<= 75 Returns records where the field value is less than or equal to 75.
20 OR 25 Returns records where the field value is either 20 or 25.
>0 AND <50
     // or //
Between 0 and 50
Returns records where the field value is between 0 and 50 (within a range of values).
< 50 OR > 100 Returns records where the field value is outside the range of values 50 and 100.
Is Null Returns records where there is no value in the field.
Is Not Null Returns records where the value is not missing in the field.
" "
(a pair of quotes)
Returns records where the field is set to blank (but not null).
Not " " Returns records where the field has a nonblank value.
" " Or Is Null Returns records where there is either no value in the field, or the field is set to a blank value.


►   Exam-style Questions

Q14.  A database table, MARKS, is created with fields StdID, StdName, Sex, DOB, Grade, Eng, Dhi and Math to store student's details.

StdID StdName Sex DOB Grade House Eng Dhi Math
2030 ATHOOF M 07-May-04 10(B) Red 70 82 94
2031 FAINAAN M 12-Apr-04 10(A) Yellow 68 77 86
2033 IYALA F 10-Oct-04 10(A) Green 89 92 95
2036 MAIHA F 13-Sep-04 10(A) Red 76 82 90
2038 SHAAIL M 08-Oct-04 10(B) Orange 54 79 58
2039 HASEEN M 14-Dec-04 10(B) Blue 46 65 49
2043 EENAASH F 10-Oct-04 10(A) Green 57 67 62
2238 MAZEENA F 16-May-04 10(A) Orange 49 62 70
2053 MAISHA F 24-Jun-04 10(B) Yellow 62 74 75
2072 DHAAEE M 31-Dec-04 10(B) Blue 50 59 88

a)  (i)  State how many records and fields are there in the above table.

Records : 10         Fields : 8

(ii)  State which field you would choose for the primary key :  StdID

Give reason of your choice :  The field "StdID" contains unique data elements, which could be used to identify each record (all data elements are different).

b)  State what data type you would choose for the following fields.

StdID : Integer
StdName : Text
Sex : Boolean (M/F)
DOB : Date/Time
Grade : Text
Eng : Integer

c)  Write the SQL statement to display student's index, name and sex in ascending order of their name, grouped by sex listing male first female next, where marks scored in english is greater than 60.

SELECT  StdID,  StdName,  Sex

FROM  MARKS

WHERE  Eng > 60

ORDER BY  Sex DESC,  StdName ;

(Note : The field which has to be grouped should be sorted first.)

What would be output of the SQL query you have written above ?

2030 M ATHOOF
2031 M FAINAAN
2033 F IYALA
2036 F MAIHA
2053 F MAISHA

d)  Write the SQL statement to display the name and marks scored in math subject of all girls of grade 10(A) in descending order of their marks in maths.

SELECT  StdName,  Math

FROM  MARKS

WHERE  Sex Like "F" AND  Grade Like "10(A)"

ORDER BY  Math DESC ;

e)  Write the SQL statement to display student's index, name and grade in descending order of their marks in dhivehi grouped by grade of all students, who has scored marks less than 45 and greater than or equal to 60 in dhivehi.

SELECT  StdID,  StdName,  Grade

FROM  MARKS

WHERE  Dhi < 45 OR Dhi >= 60

ORDER BY  Grade,  Dhi DESC ;

f)  Write the SQL statement to display student's index, name and sex in descending order of their marks in english, grouped by their gender female first, male next, who has scored marks between 60 and 75 (inclusive) in english.

SELECT  StdID,  StdName,  Sex

FROM  MARKS

WHERE  Eng >= 60 AND Eng <= 75

ORDER BY  Sex,  Eng DESC ;

(Note : Since criteria "WHERE Eng >= 60 AND Eng <= 75" includes the boundary values like 60, 61, 62,...75, you can also use the criteria, like WHERE Between 60 and 75 which will return the same records.)

g)  Write the SQL statement to display student's name only in descending order of marks in english, grouped by their grade, who has scored either marks greater than or equal to 50 in english or marks greater than 60 in dhivehi.

SELECT  StdName

FROM  MARKS

WHERE  Eng >= 50 OR Dhi > 60

ORDER BY  Grade,  Eng DESC ;

h)  Write the SQL statement to display student's name and grade, who has passed in all their three subjects with marks greater than or equal to 60.

SELECT  StdName

FROM  MARKS

WHERE  Eng >= 60 AND Dhi >= 60 AND Dhi >= 60 ;

i)  Write the SQL statement to display student's name and grade, who has scored marks greater than or equal to 85 in maths and marks greater than or equal to 60 in either english or dhivehi.

SELECT  StdName

FROM  MARKS

WHERE  Math >= 85 AND (Eng >= 60 OR Dhi >= 60) ;

j)  Write SQL statement to display student's index, name, grade, who belongs to the house other than Orange (like, Red, Green, Blue or Yellow).

SELECT  StdID,  StdName, Grade

FROM  MARKS

WHERE  House Not Like "Orange" ;

k)  Write the SQL statement to display student's name and grade, who belongs to either Red or Blue house only.

SELECT  StdID,  StdName,  Grade

FROM  MARKS

WHERE  House Like "Red" OR House Like "Blue" ;

l)  Write the SQL statement to return the name, grade and data of birth (DOB) of students who were born between 1st June 2004 and 30th October 2004.

SELECT  StdName,  Grade,  DOB

FROM  MARKS

WHERE  DOB >=  #6/1/2004#  AND  DOB <=  #10/30/2004# ;

// OR //

WHERE  DOB Between  #6/1/2004#  AND  #10/30/2004# ;

m)  Write the SQL statement to return the name, sex and total mark of english, dhivehi and math subject scored by each student of grade 10(A), listed in highest to lowest order of their total marks.

SELECT  StdName,  Sex,  (Eng + Dhi + Math) AS TotalMark

FROM  MARKS

WHERE  Grade Like "10(A)"

ORDER BY  (Eng + Dhi + Math)  DESC ;

Q15.  A database table, SALES, is used to keep a record of items made and sold by a furniture maker.

ItemNo OrderNo Notes Qty Amount Status
CH001 1921 Smith – six dining chairs 6 4500 Delivered
TB003 1921 Smith – large table 2 3200 In progress
CH001 1924 Hue - extra chairs 4 3800 Not started
CH003 1925 Easy chairs 2 3600 Cancelled
BN001 1927 Patel - replacement bence 1 1350 Not started
ST002 1931 Sola - small table 1 2400 Delivered
CH003 1927 Patel - eight dining chairs with arms 8 9600 In progress
TB003 1927 Pagel - large table 1 3450 Not started

a)  Explain why the field Item number could not be used as a primary key.

⇒  All the fields contains repeated data element, so no field can uniquely identify each record and cannot be set as primary key field.

b)  Write the SQL statement to calculate and output the amount of money collected from delivered items.

SELECT  SUM(Amount) AS TotalAmt

FROM  SALES

WHERE  Status Like "Delivered" ;

What would be the output by the above SQL query ?

TotalAmt
6900

c)  Write the SQL statement to count and output how many of the items are in progress.

SELECT  COUNT(Status) AS NoItems

FROM  SALES

WHERE  Status Like "In progress" ;

What would be the output by the above SQL query ?

NoItems
2

d)  Write the SQL statement to count and output the number of items has collected amount more than 3500.

SELECT  COUNT(Amount) AS NoItems

FROM  SALES

WHERE  Amount > 3500 ;

What would be the output by the above SQL query ?

NoItems
4

Q16.  SQL statement WHERE which specifies the condition often include values from fields, these values need to be stated in a form that matches the data type for the field.

Tick() the appropriate column to show how to provide values in criteria for a field to filter out the records.

Field data type How to provide value
Value should be enclosed in
single or double quotation mark.
Value should not be enclosed in
single or double quotation mark.
Text  
Integer  
Real  
Boolean  
Character  
Date/Time  

Note : Data type Date/Time should be enclosed in hash (#) not single or double quote.
Data type Boolean store data using binary digits 0 and 1, so it should not be enclosed in single or double quote.




* * * * * * * * *
* * * * * *
* * *
*

>>